package com.hivision.streetlight.codec;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;


public class CmdProcess {
	private final int MAIN_VERSION = 0;

	private final int SUB_VERSION = 0;

	private final int CMDCODE_INDEX = 16;

	private final int CMD_LEN_SET_BRIGHTNESS = 21;

	private final int CMD_LEN_RESET = 20;

	private final int CMD_LEN_SET_PARAMETERS = 28;

	private final int CMD_LEN_GET_PARAMETERS = 20;

	private final int CMD_LEN_GET_STATUS = 20;

	private final int CMD_LEN_UPDATE_FIRMWARE = 28;

	private final int CMD_LEN_SET_IOT = 26;

	private final int CMD_LEN_SET_SOLAR_PARAMETERS = 40;

	private final int CMD_LEN_NO_PARAMETERS_CMD = 20;

	private final int CMD_CODE_SET_BRIGHTNESS = 4;

	private final int CMD_CODE_RESET = 5;

	private final int CMD_CODE_SET_PARAMETERS = 6;

	private final int CMD_CODE_GET_PARAMETERS = 7;

	private final int CMD_CODE_GET_STATUS = 8;

	private final int CMD_CODE_UPDATE_FIRMWARE = 9;

	private final int CMD_CODE_SET_IOT = 10;

	private final int CMD_CODE_SET_SOLAR_PARAMETERS = 25;

	private final int CMD_CODE_GET_SOLAR_PARAMETERS = 26;

	private final int CMD_CODE_SET_PLAN = 27;

	private final int CMD_CODE_GET_PLAN = 28;

	private final int CMD_CODE_GET_SOLAR_STATUS = 29;

	public byte[] toByte(ObjectNode input) {
		String msgType = input.get("msgType").asText();
		int hasMore = (input.get("hasMore") != null) ? input.get("hasMore").asInt() : 0;
		String cmd = input.get("cmd").asText();
		try {
			if (msgType.equals("cloudRsp"))
				return cloudRsp2Byte(input);
			if ("SET_BRIGHTNESS".equals(cmd))
				return setBrightness2Byte(input);
			if ("RESET_DEVICE".equals(cmd))
				return resetDevice2Byte(input);
			if ("SET_PARAMETERS".equals(cmd))
				return setParameters2Byte(input);
			if ("GET_PARAMETERS".equals(cmd))
				return getParameters2Byte(input);
			if ("GET_STATUS".equals(cmd))
				return getStatus2Byte(input);
			if ("UPDATE_FIRMWARE".equals(cmd))
				return updateFirmware2Byte(input);
			if ("SET_IOT".equals(cmd))
				return setIot2Byte(input);
			if ("SET_SOLAR_PARAMETERS".equals(cmd))
				return setSolarParameters2Byte(input);
			if ("GET_SOLAR_PARAMETERS".equals(cmd))
				return executeNoParamCmd(input, 26, 20);
			if ("SET_PLAN".equals(cmd))
				return setPlan2Byte(input);
			if ("GET_PLAN".equals(cmd))
				return executeNoParamCmd(input, 28, 20);
			if ("GET_SOLAR_STATUS".equals(cmd))
				return executeNoParamCmd(input, 29, 20);
			System.out.println(String.format("error op [%s] can not found in CmdProcess", new Object[] { cmd }));
			throw new Exception(String.format("error op [%s] can not found in CmdProcess", new Object[] { cmd }));
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	private byte[] setBrightness2Byte(ObjectNode input) throws Exception {
		String uid;
		int mid, brightness;
		JsonNode paras = input.get("paras");
		int cmdCode = 4;
		if (paras.has("parameters") && paras.get("parameters").has("uid") && paras.get("parameters").has("mid")) {
			uid = paras.get("parameters").get("uid").asText();
			mid = paras.get("parameters").get("mid").asInt();
			brightness = paras.get("parameters").get("brightness").asInt();
		} else {
			uid = paras.get("uid").asText();
			mid = paras.get("mid").asInt();
			brightness = paras.get("brightness").asInt();
		}
		int reqLength = 21;
		byte[] byteRead = new byte[reqLength];
		int i = setBaseValue(byteRead, uid, mid, cmdCode, reqLength);
		byteRead[i++] = (byte)brightness;
		crc(byteRead, i, reqLength);
		return byteRead;
	}

	private byte[] resetDevice2Byte(ObjectNode input) throws Exception {
		String uid;
		int mid;
		JsonNode paras = input.get("paras");
		int cmdCode = 5;
		if (paras.has("parameters") && paras.get("parameters").has("uid") && paras.get("parameters").has("mid")) {
			uid = paras.get("parameters").get("uid").asText();
			mid = paras.get("parameters").get("mid").asInt();
		} else {
			uid = paras.get("uid").asText();
			mid = paras.get("mid").asInt();
		}
		int reqLength = 20;
		byte[] byteRead = new byte[reqLength];
		int i = setBaseValue(byteRead, uid, mid, cmdCode, reqLength);
		crc(byteRead, i, reqLength);
		return byteRead;
	}

	private byte[] setParameters2Byte(ObjectNode input) throws Exception {
		String uid;
		int mid;
		JsonNode paras = input.get("paras");
		int cmdCode = 6;
		int heartBeatIntervel = 0;
		int uploadInterval = 0;
		double guoYaVoltage = 0.0D;
		double qianYaVoltage = 0.0D;
		if (paras.has("parameters") && paras.get("parameters").has("uid") && paras.get("parameters").has("mid")) {
			uid = paras.get("parameters").get("uid").asText();
			mid = paras.get("parameters").get("mid").asInt();
			heartBeatIntervel = paras.get("parameters").get("heartBeatIntervel").asInt();
			uploadInterval = paras.get("parameters").get("uploadInterval").asInt();
			guoYaVoltage = paras.get("parameters").get("guoYaVoltage").asDouble();
			qianYaVoltage = paras.get("parameters").get("qianYaVoltage").asDouble();
		} else {
			uid = paras.get("uid").asText();
			mid = paras.get("mid").asInt();
			heartBeatIntervel = paras.get("heartBeatIntervel").asInt();
			uploadInterval = paras.get("uploadInterval").asInt();
			guoYaVoltage = paras.get("maxVoltagethreshold").asDouble();
			qianYaVoltage = paras.get("minVoltagethreshold").asDouble();
		}
		int reqLength = 28;
		byte[] byteRead = new byte[reqLength];
		int i = setBaseValue(byteRead, uid, mid, cmdCode, reqLength);
		byte[] heartBeatIntervelByte = Utilty.getInstance().int2Bytes(heartBeatIntervel, 2);
		byteRead[i++] = heartBeatIntervelByte[0];
		byteRead[i++] = heartBeatIntervelByte[1];
		byte[] ploadIntervalByte = Utilty.getInstance().int2Bytes(uploadInterval, 2);
		byteRead[i++] = ploadIntervalByte[0];
		byteRead[i++] = ploadIntervalByte[1];
		byte[] guoYaVoltageByte = Utilty.getInstance().int2Bytes((new Double(guoYaVoltage * 10.0D)).intValue(), 2);
		byteRead[i++] = guoYaVoltageByte[0];
		byteRead[i++] = guoYaVoltageByte[1];
		byte[] qianYaVoltageByte = Utilty.getInstance().int2Bytes((new Double(qianYaVoltage * 10.0D)).intValue(), 2);
		byteRead[i++] = qianYaVoltageByte[0];
		byteRead[i++] = qianYaVoltageByte[1];
		crc(byteRead, i, reqLength);
		return byteRead;
	}

	private byte[] getParameters2Byte(ObjectNode input) throws Exception {
		String uid;
		int mid;
		JsonNode paras = input.get("paras");
		int cmdCode = 7;
		if (paras.has("parameters") && paras.get("parameters").has("uid") && paras.get("parameters").has("mid")) {
			uid = paras.get("parameters").get("uid").asText();
			mid = paras.get("parameters").get("mid").asInt();
		} else {
			uid = paras.get("uid").asText();
			mid = paras.get("mid").asInt();
		}
		int reqLength = 20;
		byte[] byteRead = new byte[reqLength];
		int i = setBaseValue(byteRead, uid, mid, cmdCode, reqLength);
		crc(byteRead, i, reqLength);
		return byteRead;
	}

	private byte[] getStatus2Byte(ObjectNode input) throws Exception {
		String uid;
		int mid;
		JsonNode paras = input.get("paras");
		int cmdCode = 8;
		if (paras.has("parameters") && paras.get("parameters").has("uid") && paras.get("parameters").has("mid")) {
			uid = paras.get("parameters").get("uid").asText();
			mid = paras.get("parameters").get("mid").asInt();
		} else {
			uid = paras.get("uid").asText();
			mid = paras.get("mid").asInt();
		}
		int reqLength = 20;
		byte[] byteRead = new byte[reqLength];
		int i = setBaseValue(byteRead, uid, mid, cmdCode, reqLength);
		crc(byteRead, i, reqLength);
		return byteRead;
	}

	private byte[] updateFirmware2Byte(ObjectNode input) throws Exception {
		String uid;
		int mid;
		String softWareStr;
		int softPackLength;
		byte[] softWareByte;
		JsonNode paras = input.get("paras");
		int cmdCode = 9;
		if (paras.has("parameters") && paras.get("parameters").has("uid") && paras.get("parameters").has("mid")) {
			uid = paras.get("parameters").get("uid").asText();
			mid = paras.get("parameters").get("mid").asInt();
			softWareStr = paras.get("parameters").get("softWareStr").asText();
			softPackLength = paras.get("parameters").get("softPackLength").asInt();
		} else {
			uid = paras.get("uid").asText();
			mid = paras.get("mid").asInt();
			softWareStr = paras.get("softWareStr").asText();
			softPackLength = paras.get("softPackLength").asInt();
		}
		try {
			softWareByte = Base64.getDecoder().decode(softWareStr);
		} catch (Exception e) {
			return null;
		}
		int reqLength = 28 + softPackLength;
		byte[] byteRead = new byte[reqLength];
		int i = setBaseValue(byteRead, uid, mid, cmdCode, reqLength);
		int offset = paras.get("offset").asInt();
		byte[] offsetByte = Utilty.getInstance().int2Bytes(offset, 4);
		byteRead[i++] = offsetByte[0];
		byteRead[i++] = offsetByte[1];
		byteRead[i++] = offsetByte[2];
		byteRead[i++] = offsetByte[3];
		byte[] softPackLengthByte = Utilty.getInstance().int2Bytes(softPackLength, 4);
		byteRead[i++] = softPackLengthByte[0];
		byteRead[i++] = softPackLengthByte[1];
		byteRead[i++] = softPackLengthByte[2];
		byteRead[i++] = softPackLengthByte[3];
		System.arraycopy(softWareByte, 0, byteRead, i, softPackLength);
		i += softPackLength;
		crc(byteRead, i, reqLength);
		return byteRead;
	}

	private byte[] setIot2Byte(ObjectNode input) throws Exception {
		String uid;
		int mid;
		String iotIp;
		int iotPort;
		JsonNode paras = input.get("paras");
		int cmdCode = 10;
		if (paras.has("parameters") && paras.get("parameters").has("uid") && paras.get("parameters").has("mid")) {
			uid = paras.get("parameters").get("uid").asText();
			mid = paras.get("parameters").get("mid").asInt();
			iotIp = paras.get("parameters").get("iotIp").asText();
			iotPort = paras.get("parameters").get("iotPort").asInt();
		} else {
			uid = paras.get("uid").asText();
			mid = paras.get("mid").asInt();
			iotIp = paras.get("iotIp").asText();
			iotPort = paras.get("iotPort").asInt();
		}
		int reqLength = 26;
		byte[] byteRead = new byte[reqLength];
		int i = setBaseValue(byteRead, uid, mid, cmdCode, reqLength);
		String[] iotIpPiece = iotIp.split("[.]");
		byteRead[i++] = (byte)Integer.valueOf(iotIpPiece[0]).intValue();
		byteRead[i++] = (byte)Integer.valueOf(iotIpPiece[1]).intValue();
		byteRead[i++] = (byte)Integer.valueOf(iotIpPiece[2]).intValue();
		byteRead[i++] = (byte)Integer.valueOf(iotIpPiece[3]).intValue();
		byte[] iotPortByte = Utilty.getInstance().int2Bytes(iotPort, 2);
		byteRead[i++] = iotPortByte[0];
		byteRead[i++] = iotPortByte[1];
		crc(byteRead, i, reqLength);
		return byteRead;
	}

	private byte[] cloudRsp2Byte(ObjectNode input) {
		byte[] request = Utilty.hexString2Bytes(input.get("request").asText());
		int errcode = input.get("errcode").asInt();
		int requestCmdCode = Utilty.getInstance().bytes2Int(request, 16, 2);
		if (requestCmdCode == 2) {
			request[16] = 1;
			request[17] = 2;
			byte[] tt = new byte[request.length - 2];
			System.arraycopy(request, 0, tt, 0, tt.length);
			int crc = CRC16M.calculateCrc16(tt, tt.length);
			byte[] byteCrc = Utilty.getInstance().int2Bytes(crc, 2);
			request[request.length - 2] = byteCrc[0];
			request[request.length - 1] = byteCrc[1];
			return request;
		}
		return null;
	}

	private byte[] setSolarParameters2Byte(ObjectNode input) throws Exception {
		JsonNode paras = input.get("paras");
		int cmdCode = 25;
		JsonNode data = paras;
		if (paras.has("parameters"))
			data = paras.get("parameters");
		String uid = data.get("uid").asText();
		int mid = data.get("mid").asInt();
		int reportInterval = data.get("reportInterval").asInt();
		Double qianYaThreshold = Double.valueOf(data.get("qianYaThreshold").asDouble());
		Double guoYaThreshold = Double.valueOf(data.get("guoYaThreshold").asDouble());
		Double guoLiuThreshold = Double.valueOf(data.get("guoLiuThreshold").asDouble());
		Double guoWenThreshold = Double.valueOf(data.get("guoWenThreshold").asDouble());
		Double tianLiangThreshold = Double.valueOf(data.get("tianLiangThreshold").asDouble());
		Double tianHeiThreshold = Double.valueOf(data.get("tianHeiThreshold").asDouble());
		int batterType = data.get("batterType").asInt();
		int batterNumber = data.get("batterNumber").asInt();
		Double ledRatedCurrent = Double.valueOf(data.get("ledRatedCurrent").asDouble());
		int reqLength = 40;
		byte[] byteRead = new byte[reqLength];
		int i = setBaseValue(byteRead, uid, mid, cmdCode, reqLength);
		byte[] reportIntervalByte = Utilty.getInstance().int2Bytes(reportInterval, 2);
		byteRead[i++] = reportIntervalByte[0];
		byteRead[i++] = reportIntervalByte[1];
		byte[] qianYaThresholdByte = Utilty.getInstance().int2Bytes((new Double(qianYaThreshold.doubleValue() * 10.0D)).intValue(), 2);
		byteRead[i++] = qianYaThresholdByte[0];
		byteRead[i++] = qianYaThresholdByte[1];
		byte[] guoYaThresholdByte = Utilty.getInstance().int2Bytes((new Double(guoYaThreshold.doubleValue() * 10.0D)).intValue(), 2);
		byteRead[i++] = guoYaThresholdByte[0];
		byteRead[i++] = guoYaThresholdByte[1];
		byte[] guoLiuThresholdByte = Utilty.getInstance().int2Bytes((new Double(guoLiuThreshold.doubleValue() * 10.0D)).intValue(), 2);
		byteRead[i++] = guoLiuThresholdByte[0];
		byteRead[i++] = guoLiuThresholdByte[1];
		byte[] guoWenThresholdByte = Utilty.getInstance().int2Bytes((new Double(guoWenThreshold.doubleValue() * 10.0D)).intValue(), 2);
		byteRead[i++] = guoWenThresholdByte[0];
		byteRead[i++] = guoWenThresholdByte[1];
		byte[] tianLiangThresholdByte = Utilty.getInstance().int2Bytes((new Double(tianLiangThreshold.doubleValue() * 10.0D)).intValue(), 2);
		byteRead[i++] = tianLiangThresholdByte[0];
		byteRead[i++] = tianLiangThresholdByte[1];
		byte[] tianHeiThresholdByte = Utilty.getInstance().int2Bytes((new Double(tianHeiThreshold.doubleValue() * 10.0D)).intValue(), 2);
		byteRead[i++] = tianHeiThresholdByte[0];
		byteRead[i++] = tianHeiThresholdByte[1];
		byte[] batterTypeByte = Utilty.getInstance().int2Bytes(batterType, 2);
		byteRead[i++] = batterTypeByte[0];
		byteRead[i++] = batterTypeByte[1];
		byte[] batterNumberByte = Utilty.getInstance().int2Bytes(batterNumber, 2);
		byteRead[i++] = batterNumberByte[0];
		byteRead[i++] = batterNumberByte[1];
		byte[] ledRatedCurrentByte = Utilty.getInstance().int2Bytes((new Double(ledRatedCurrent.doubleValue() * 10.0D)).intValue(), 2);
		byteRead[i++] = ledRatedCurrentByte[0];
		byteRead[i++] = ledRatedCurrentByte[1];
		crc(byteRead, i, reqLength);
		return byteRead;
	}

	private byte[] setPlan2Byte(ObjectNode input) throws Exception {
		JsonNode paras = input.get("paras");
		int cmdCode = 25;
		JsonNode data = paras;
		if (paras.has("parameters"))
			data = paras.get("parameters");
		String uid = data.get("uid").asText();
		int mid = data.get("mid").asInt();
		int planNumber = data.get("planNumber").asInt();
		int reqLength = 21 + planNumber * 3;
		byte[] byteRead = new byte[reqLength];
		int i = setBaseValue(byteRead, uid, mid, cmdCode, reqLength);
		byte[] planNumberByte = Utilty.getInstance().int2Bytes(planNumber, 1);
		byteRead[i++] = planNumberByte[0];
		JsonNode planArray = data.get("plans");
		if (planArray.isArray())
			for (JsonNode it : planArray) {
				byteRead[i++] = (byte)it.get("hour").asInt();
				byteRead[i++] = (byte)it.get("minute").asInt();
				byteRead[i++] = (byte)it.get("brightness").asInt();
			}
		crc(byteRead, i, reqLength);
		return byteRead;
	}

	private byte[] executeNoParamCmd(ObjectNode input, int cmdCode, int reqLength) throws Exception {
		JsonNode paras = input.get("paras");
		JsonNode data = paras;
		if (paras.has("parameters"))
			data = paras.get("parameters");
		String uid = data.get("uid").asText();
		int mid = data.get("mid").asInt();
		byte[] byteRead = new byte[reqLength];
		int i = setBaseValue(byteRead, uid, mid, cmdCode, reqLength);
		crc(byteRead, i, reqLength);
		return byteRead;
	}

	private int setBaseValue(byte[] sendByte, String uid, int mid, int cmdCode, int msgLength) throws Exception {
		int i = 0;
		sendByte[i++] = 64;
		sendByte[i++] = 35;
		byte[] byteLen = Utilty.getInstance().int2Bytes(msgLength - 4, 2);
		sendByte[i++] = byteLen[0];
		sendByte[i++] = byteLen[1];
		sendByte[i++] = 0;
		sendByte[i++] = 0;
		if (uid.contains("Broadcast")) {
			sendByte[i++] = -1;
			sendByte[i++] = -1;
			sendByte[i++] = -1;
			sendByte[i++] = -1;
			sendByte[i++] = -1;
			sendByte[i++] = -1;
			sendByte[i++] = -1;
			sendByte[i++] = -1;
		} else {
			byte[] byteUid = uid.getBytes();
			byte b;
			int j;
			byte[] arrayOfByte1;
			for (j = (arrayOfByte1 = byteUid).length, b = 0; b < j; ) {
				byte b1 = arrayOfByte1[b];
				sendByte[i++] = b1;
				b++;
			}
		}
		if (Utilty.getInstance().isValidofMid(mid)) {
			byte[] byteMid = Utilty.getInstance().int2Bytes(mid, 2);
			sendByte[i++] = byteMid[0];
			sendByte[i++] = byteMid[1];
		} else {
			sendByte[i++] = -1;
			sendByte[i++] = -1;
		}
		byte[] byteCmdCode = Utilty.getInstance().int2Bytes(cmdCode, 2);
		sendByte[i++] = byteCmdCode[0];
		sendByte[i++] = byteCmdCode[1];
		return i;
	}

	private byte[] crc(byte[] data, int startPos, int dataLen) {
		byte[] tt = new byte[dataLen - 2];
		System.arraycopy(data, 0, tt, 0, dataLen - 2);
		int crc = CRC16M.calculateCrc16(tt, data.length - 2);
		byte[] byteCrc = Utilty.getInstance().int2Bytes(crc, 2);
		data[startPos++] = byteCrc[0];
		data[startPos++] = byteCrc[1];
		return data;
	}

	public static void main(String[] args) throws Exception {
		String cmdStr1 = "{\"identifier\": \"123\",\"msgType\":\"cloudReq\",\"serviceId\": \"DeviceConfig\",\"mid\": 2,\"cmd\":\"SET_IOT\", \"paras\": {\"parameters\": {\"mid\": 2,\"uid\": \"HV-TEST0\",\"cmdCode\": 10,\"iotIp\": \"192.168.1.20\",\"iotPort\":\"2000\"}},\"hasMore\": 0}";
		String cmdStr2 = "{\"identifier\": \"123\",\"msgType\":\"cloudReq\",\"serviceId\": \"Control\",\"mid\": 7,\"cmd\":\"SET_BRIGHTNESS\", \"paras\": {\"uid\":\"HV-TEST3\",\"brightness\":\"38\",\"mid\":\"7\"},\"hasMore\": 0}";
		String cmdStr3 = "{\"identifier\":\"HV-TEST3\", \"serviceId\":\"DeviceConfig\", \"cmd\":\"SET_PARAMETERS\", \"paras\":\"{\"parameters\":{\"uid\":\"HV-TEST3\",\"heartBeatIntervel\":0,\"uploadInterval\":0,\"maxVoltagethreshold\":250,\"cmdCode\":6,\"minVoltagethreshold\":20}}\", \"hasMore\":\"0\",\"mId\":\"227227\"}";
		String cmdStr4 = "{\"expireTime\": 0,\"appId\": \"6tVtZ4zabJgSo5l15d9EUJg1cZMa\",\"callbackUrl\": \"http://121.31.127.6:8020/na/iocm/devNotify/v1.1.0/reportCmdExecResult\",\"deviceId\": \"4f72b00c-2d9c-4ee7-bb97-80173ab3e663\",\"command\": {\"method\": \"UPDATE_FIRMWARE\",\"mid\": \"85\",\"paras\": {\"softWareStr\": \"Vcy7qjUwMDF4UgAAAGAACG1bMlD///////////////+wSwAgmWEACCF6AAgZeQAIHXoACMV1AAglkgAIAAAAAAAAAAAAAAAAAAAAAMmJAAjJdQAIAAAAACdiAAjtigAIs2EACLNhAAizYQAIs2EACLNhAAizYQAIs2EACMt1AAjddQAIs2EACO91AAizYQAIs2EACLNhAAizYQAIs2EACLNhAAizYQAIs2EACLNhAAizYQAIs2EACLNhAAgBdgAIs2EACLNhAAizYQAIs2EACLNhAAizYQAIs2EACLNhAAizYQAIs2EACLNhAAizYQAIs2EACO2OAAhBjwAIlY8ACA==\",\"uid\": \"Broadcast\",\"softPackLength\": 256,\"offset\": 0,\"mid\": 86},\"serviceId\": \"DeviceConfig\"}}";
		List<String> cmdList = new ArrayList<>();
		cmdList.add(cmdStr4);
		StringBuffer b1 = new StringBuffer();
		b1.append("\t{                                                                                                                                                                                                                 ");
		b1.append("\t\t\"identifier\": \"123\",                                                ");
		b1.append("\t\t\"msgType\": \"cloudReq\",                                              ");
		b1.append("\t\t\"serviceId\": \"DeviceConfig\",                  ");
		b1.append("\t\t\"mid\": 2,                                                             ");
		b1.append("\t\t\"cmd\": \"SET_SOLAR_PARAMETERS\",                                       ");
		b1.append("\t\t\"paras\": {                                                                                                                                                                     ");
		b1.append("\t\t\t\t\"uid\": \"123456\",             ");
		b1.append("\t\t\"mid\": 2,                                                                                                                                                                                                                                                                                                                                                                            ");
		b1.append("\t\t\t\t\"reportInterval\": 20, ");
		b1.append("\t\t\t\t\"qianYaThreshold\": 100,                                                                                                                                                   ");
		b1.append("\t\t\t\t\"guoYaThreshold\": 110,                                                                                                                                                 ");
		b1.append("\t\t\t\t\"guoLiuThreshold\": 5,                                                                                                                                                           ");
		b1.append("\t\t\t\t\"guoWenThreshold\": 40,                                            ");
		b1.append("\t\t\t\t\"tianLiangThreshold\": 120,                                       ");
		b1.append("\t\t\t\t\"tianHeiThreshold\": 130,                                                                                                                                                               ");
		b1.append("\t\t\t\t\"batterType\": 0,                                                                                                                                                               ");
		b1.append("\t\t\t\t\"batterNumber\": 4,                                                                                                                                                               ");
		b1.append("\t\t\t\t\"ledRatedCurrent\": 3                                                                                                                                                               ");
		b1.append("\t\t\t},                                                                                                                                                                           ");
		b1.append("\t\t\"hasMore\": 0                                                                                                                                                                   ");
		b1.append("\t}                                                                                                                                                                                                                                                                                                           ");
		ProtocolAdapterImpl protocolAdapterImpl = new ProtocolAdapterImpl();
		ObjectMapper mapper = new ObjectMapper();
		ObjectNode readNode = (ObjectNode)mapper.readTree(b1.toString());
		byte[] b = protocolAdapterImpl.encode(readNode);
		System.out.println(Utilty.parseByte2HexStr(b));
		StringBuffer bf = new StringBuffer();
		byte b2;
		int i;
		byte[] arrayOfByte1;
		for (i = (arrayOfByte1 = b).length, b2 = 0; b2 < i; ) {
			byte it = arrayOfByte1[b2];
			bf.append(",").append(it);
			b2++;
		}
		System.out.println(bf.toString());
	}
}
